package Logical;

import java.awt.Dimension;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JPanel;

import MulThread.TImeCounterDelegate;
import MulThread.TimeCounter;

public class Card extends JButton implements ActionListener, TImeCounterDelegate{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	public int ID ;
	public String  IDString;
	public CardsQueue cardsSelectedQueue;
	public int  CardNumber=0;
	//card tag
	public boolean canReverse = true;
	
	//card status 
	public static int CARD_FORWARD_TAG = 1;
	public static int CARD_BACK_TAG = -1;
	public long TOGGLE_START_TIME;
	
	public int CARD_STATUS = CARD_BACK_TAG;
	
	//card content IMage
	private ImageIcon frontIcon;
	private ImageIcon backIcon;
	
	
	
	public Card(int card_id){
		super();
		this.ID = card_id;
		this.IDString = String.valueOf(this.ID);
		this.setText(IDString);
		
		//setting style of Jbutton
		
	}
	
	public void setCardnumber(int size){
		this.CardNumber = size*size;
	}
	
	/*
	 * set Image 
	 */
	private void changeBackGround(){
		if (CARD_STATUS == CARD_BACK_TAG){
			this.setIcon(backIcon);
			this.setRolloverEnabled(true);
			this.setRolloverIcon(new ImageIcon("res/img/back_roller.png"));
			
		}else{
			this.setRolloverEnabled(false);
			this.setIcon(frontIcon);
		}
	}
	
	public JPanel preferenceSetting(){
		System.out.println("setting");
		JPanel panel = new JPanel();
		this.setPreferredSize(new Dimension(104, 185));
		panel.setMaximumSize(new Dimension(110,190));
		panel.setMinimumSize(new Dimension(100,180));
		
		frontIcon = new ImageIcon("res/img/"+String.valueOf(ID)+".png");
		backIcon = new ImageIcon("res/img/back.png");
		this.setMargin(new Insets(0, 10, 0, 0));
		
		changeBackGround();
//		this.setPressedIcon();
		panel.add(this);
//		System.out.println(panel.getHeight());
		return panel;
	}
	public void setCardQueue(CardsQueue queue){
		cardsSelectedQueue = queue;
	}
	
	public void setCardID(int id){
		this.ID = id;
		this.IDString = String.valueOf(id);
		this.setText(IDString);
	}
	
	
	public void cardReverse (){
		if (! canReverse){
			return;
		}
		
		long time = System.currentTimeMillis();
		if(this.CARD_STATUS == CARD_BACK_TAG){
			System.out.println("to forward !"+String.valueOf(CARD_STATUS));
			this.setCardStatus(CARD_FORWARD_TAG);
			this.setText(this.IDString);
			this.TOGGLE_START_TIME = time;
		}else if(this.CARD_STATUS == CARD_FORWARD_TAG){
			System.out.println("to Back !");
			this.setCardStatus(CARD_BACK_TAG);
			this.setText("?");
		}
		//mark card witch taped
		MarkCard();
		
	}
	public void toBack(){
		if (canReverse){
			this.setCardStatus(CARD_BACK_TAG);
			this.setText("?");
		}
		//mark card witch taped.
		MarkCard();
		
	}
	
	public  void DisplayID(){
		if (CARD_STATUS == CARD_FORWARD_TAG){
			this.setText(this.IDString);
			System.out.println(this.ID);
		}else{
			this.setText("?");
		}
		changeBackGround();
	}
	
	
	protected void  setCardStatus(int status) {
		this.CARD_STATUS = status;
	
	}
	
	public void MarkCard(){
		if (canReverse){
			if (CARD_STATUS == CARD_FORWARD_TAG){
				cardsSelectedQueue.CardSelected(this.ID, this,CardNumber);
			}else{
				cardsSelectedQueue.CardMissed(this.ID);
			}
		}
		changeBackGround();
	}
	
	/*   ### - ActionListener -methods
	 * (non-Javadoc)
	 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
	 *  this method to listen a mouse toggle.
  	 */
	@Override
	public void actionPerformed(ActionEvent event) {
		// TODO Auto-generated method stub
		
		Card thisCard = (Card) event.getSource(); 
		
		thisCard.cardReverse();
		
		
		//initialization time-counter
		if (CARD_STATUS == CARD_FORWARD_TAG){
			//setting time counter
			TimeCounter counter = new TimeCounter(thisCard.TOGGLE_START_TIME);
			counter.setDelegate(this);
			counter.setTimeDelay(1300);
			
			Thread timerCounterThread = new Thread(counter); 
			timerCounterThread.start();
		}
		
	}

	/*
	 *  ### - TImeCounterDelegate method
	 */
	@Override
	public void Recived(int Tag) {
		// TODO Auto-generated method stub
		this.toBack();
		
	}
	
	
	public void displayFront (){
		this.setIcon(frontIcon);
	}
	public void displayBack(){
		this.setIcon(backIcon);
	}
	
	
	
}
